package com.zy.qudadid.presenter;

import com.zy.qudadid.model.Res;
import com.zy.qudadid.model.User;
import com.zy.qudadid.network.Const;
import com.zy.qudadid.network.Net;
import com.zy.qudadid.presenter.base.BasePresenterImp;
import com.zy.qudadid.ui.view.LoginView;
import com.zy.qudadid.utils.UserUtil;

import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ASUS on 2017/2/15.
 */

public class LoginPresenter extends BasePresenterImp<LoginView>{

    public void Login(String data){
        Subscription subscription = Net.getService()
                .Login(data)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Res<User>>() {

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.exception();
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Res<User> res) {
                        if (res.code == Const.OK) {
                            new UserUtil(view.getContext()).putUser(res.datas);
                            view.loginSuccess();
                        } else {
                            view.loginError(res.message);
                        }
                    }
                });

        addSubscription(subscription);
    }
}
